package com.comdosoft.comdoandroidbase.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.comdosoft.comdoandroidbase.R;

import java.util.List;

/**
 * Created by yewei on 2015/9/18.
 */
public class PullToRefreshAdapter extends BaseAdapter {

    private Context mContext;
    private List<String> mList;
    private LayoutInflater mInflater;

    public PullToRefreshAdapter(Context context, List<String> list) {
        this.mContext = context;
        this.mList = list;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (mList == null)
            return 0;
        else
            return mList.size();
    }

    @Override
    public String getItem(int position) {
        if (mList == null || position > mList.size() - 1)
            return null;
        else
            return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.fragment_pulltorefresh_item, parent, false);
            viewHolder.tv_item = (TextView) convertView.findViewById(R.id.tv_item);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv_item.setText(getItem(position));
        return convertView;
    }

    static class ViewHolder {
        TextView tv_item;
    }
}
